package topcoder;

public class RedAndGreen {

	private boolean sat(String s){
		boolean green = false;
		for (int i = 0; i < s.length(); i++) {
			if(green && s.charAt(i) == 'R')
				return false;
			if(s.charAt(i) == 'G')
				green = true;
		}
		return true;
	}
	
	private String flip(String s, int i){
		StringBuilder sb = new StringBuilder(s);
		if(s.charAt(i) == 'R')
			sb.setCharAt(i, 'G');
		else
			sb.setCharAt(i, 'R');
		return sb.toString();
	}
	
	private int repaint(String s, int i){
		if(sat(s))
			return 0;
		if(i == s.length())
			return Integer.MAX_VALUE - s.length();
		return Math.min(repaint(s, i+1), repaint(flip(s,i),i+1)+1);
	}
	
	public int minPaints(String row){
		return repaint(row, 0);
	}
}
